package org.ym.tools;


import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;

/**
 * @author ym
 * @date 2022-4-07
 */
public class ReflectTool {

    private ReflectTool(){}

    /**
     * 获取字段
     * @param fieldName 名称
     * @param c 类
     * @return 数据
     */
    public static Field getField(String fieldName,Class<?> c){
        Field result = null;
        if(StringUtils.isNotBlank(fieldName) && c != null){
            try {
                result = c.getDeclaredField(fieldName);
            } catch (NoSuchFieldException e) {
                // doNothing
            }
        }
        return result;
    }

    /**
     * 判断是否为空
     * @param field Field
     * @param model 对象数据
     * @return true/false
     */
    public static boolean checkNull(Field field,Object model){
        boolean isNull = true;
        if(field != null && model != null){
            try {
                field.setAccessible(true);
                Object o = field.get(model);
                if(o != null){
                    if(field.getType() == String.class){
                        isNull = StringUtils.isBlank(o.toString());
                    }else{
                        isNull = false;
                    }
                }
            } catch (IllegalAccessException e) {
                // doNothing
            }
        }
        return isNull;
    }
}
